//
//  ZSImageBrowser.m
//  CurriculumFactory
//
//  Created by apple on 2020/3/25.
//

#import "ZSImageBrowser.h"
@interface ZSImageBrowser ()<QMUIImagePreviewViewDelegate>
@property(nonatomic, strong) QMUIImagePreviewView *imagePreviewView;
@property(nonatomic, strong) NSArray<UIImage *> *images;

@end
@implementation ZSImageBrowser
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self uiSet];
    }
    return self;
}
- (void)uiSet {
   
        
}
- (void)setUrlImages:(NSArray *)urlImages {
    _urlImages=urlImages;
    self.imagePreviewView = [[QMUIImagePreviewView alloc] init];
    self.imagePreviewView.delegate = self;
    self.imagePreviewView.loadingColor = UIColorGray;// 设置每张图片里的 loading 的颜色，需根据业务场景来修改
    self.imagePreviewView.collectionViewLayout.minimumLineSpacing = 0;// 去掉每张图片之间的间隙
    [self addSubview:self.imagePreviewView];
}
#pragma mark - <QMUIImagePreviewViewDelegate>
- (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView {
    return self.urlImages.count;
}

- (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index {
    zoomImageView.contentMode = UIViewContentModeScaleAspectFit;
    UIImageView * imageView =[UIImageView new];
    NSString * urlstr = self.urlImages[index];
    zoomImageView.image = nil; // 第 2 张图将图片清空，模拟延迟加载的场景
    [zoomImageView showLoading];// 显示 loading（loading 也可与图片同时显示）
    [imageView sd_setImageWithURL:[NSURL URLWithString:urlstr] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        // 由于 cell 是复用的，所以之前的 zoomImageView 可能已经被用于显示其他 index 的图片了，所以这里要重新判断一下 index
                   NSUInteger indexForZoomImageView = [imagePreviewView indexForZoomImageView:zoomImageView];
                   if (indexForZoomImageView == index) {
                       zoomImageView.image = self.images[index];
                       [zoomImageView hideEmptyView];// 把 loading 隐藏掉
                   }
        
    }];
//    if (index == 1) {
//        zoomImageView.image = nil; // 第 2 张图将图片清空，模拟延迟加载的场景
//        [zoomImageView showLoading];// 显示 loading（loading 也可与图片同时显示）
//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//
//            // 由于 cell 是复用的，所以之前的 zoomImageView 可能已经被用于显示其他 index 的图片了，所以这里要重新判断一下 index
//            NSUInteger indexForZoomImageView = [imagePreviewView indexForZoomImageView:zoomImageView];
//            if (indexForZoomImageView == index) {
//                zoomImageView.image = self.images[index];
//                [zoomImageView hideEmptyView];// 把 loading 隐藏掉
//            }
//        });
//    } else {
//        // 设置图片，此时会按默认的缩放来显示（所谓的默认缩放指如果图片比容器小则显示原大小，如果图片比容器大，则缩放到完整显示图片）
//        zoomImageView.image = self.images[index];
//
////        [zoomImageView]
//        [zoomImageView hideEmptyView];
//    }
}

- (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index {
    return QMUIImagePreviewMediaTypeImage;
}
@end
